package com.laomou.password;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class Tool implements View.OnClickListener {
    private WindowManager wm;
    private WindowManager.LayoutParams wmParams;
    private View mainscreen;
    private boolean isshow;
    private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    private OneKeyLoginListener mOneKeyLoginListener;

    public interface OneKeyLoginListener {
        void OneKeyLogin(boolean login);
    }

    public Tool(Context context) {
        initAssistiveView(context);
        initLayoutParams();
        setOnTouchListener();
    }

    private void initAssistiveView(Context context) {
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mainscreen = LayoutInflater.from(context).inflate(R.layout.screen_main, null);
    }

    private void initLayoutParams() {
        wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        wmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.CENTER;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.format = PixelFormat.RGBA_8888;
    }

    private void setOnTouchListener() {
        mainscreen.findViewById(R.id.one_key_login).setOnClickListener(this);
        mainscreen.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                x = event.getRawX();
                y = event.getRawY();

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mTouchStartX = event.getX();
                        mTouchStartY = event.getY() + mainscreen.getHeight() / 2;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        updateViewPosition();
                        break;
                    case MotionEvent.ACTION_UP:
                        updateViewPosition();
                        mTouchStartX = mTouchStartY = 0;
                        break;
                }
                return false;
            }

        });
    }

    private void updateViewPosition() {
        wmParams.x = (int) (x - mTouchStartX);
        wmParams.y = (int) (y - mTouchStartY);
        wm.updateViewLayout(mainscreen, wmParams);
    }

    public void setOneKeyLoginListener(OneKeyLoginListener listener) {
        mOneKeyLoginListener = listener;
    }

    public void show() {
        if (!isshow) {
            wm.addView(mainscreen, wmParams);
            isshow = true;
        }
    }

    public void hide() {
        if (isshow) {
            wm.removeView(mainscreen);
            isshow = false;
        }
    }

    @Override
    public void onClick(View v) {
        if (mOneKeyLoginListener != null) {
            mOneKeyLoginListener.OneKeyLogin(true);
        }
    }
}
